package com.ruoyi.problem.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.problem.domain.ProblemAnswer;
import com.ruoyi.problem.domain.ProblemConsult;
import com.ruoyi.problem.service.IProblemAnswerService;
import com.ruoyi.problem.service.IProblemConsultService;
import com.ruoyi.system.domain.SystemDivine;
import com.ruoyi.system.mapper.SystemDivineMapper;
import com.ruoyi.thread.TaskAsync;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 卡牌问题咨询Controller
 * 
 * @author DEREK
 * @date 2023-08-03
 */
@RestController
@RequestMapping("/problem/consult")
public class ProblemConsultController extends BaseController
{
    @Autowired
    private IProblemConsultService problemConsultService;
    @Autowired
    private SystemDivineMapper systemDivineMapper;
    @Autowired
    private IProblemAnswerService problemAnswerService;
    @Autowired
    private TaskAsync taskAsync;
    
    
    /**
     * 查询卡牌问题咨询列表
     */
    @PreAuthorize("@ss.hasPermi('problem:consult:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProblemConsult problemConsult)
    {
        startPage();
        List<ProblemConsult> list = problemConsultService.selectProblemConsultList(problemConsult);
        list.stream().forEach(consult ->{
        	// 占卜结果
        	List<String> asList = Arrays.asList(consult.getDid().split(","));
			List<SystemDivine> divineList = systemDivineMapper.selectList(new LambdaQueryWrapper<SystemDivine>().in(SystemDivine::getId, asList.stream().map(Long::valueOf).collect(Collectors.toList())));
			if(CollectionUtil.isNotEmpty(divineList)) {
				consult.setDivineList(divineList.stream().map(SystemDivine::getExtend).collect(Collectors.toList()));
			}
        });
        return getDataTable(list);
    }

    /**
     * 导出卡牌问题咨询列表
     */
    @PreAuthorize("@ss.hasPermi('problem:consult:export')")
    @Log(title = "卡牌问题咨询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProblemConsult problemConsult)
    {
        List<ProblemConsult> list = problemConsultService.selectProblemConsultList(problemConsult);
        ExcelUtil<ProblemConsult> util = new ExcelUtil<ProblemConsult>(ProblemConsult.class);
        util.exportExcel(response, list, "卡牌问题咨询数据");
    }

    /**
     * 获取卡牌问题咨询详细信息
     */
    @PreAuthorize("@ss.hasPermi('problem:consult:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(problemConsultService.getById(id));
    }

    /**
     * 新增卡牌问题咨询
     */
    @PreAuthorize("@ss.hasPermi('problem:consult:add')")
    @Log(title = "卡牌问题咨询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProblemConsult problemConsult)
    {
        return toAjax(problemConsultService.save(problemConsult));
    }

    /**
     * 修改卡牌问题咨询
     */
    @PreAuthorize("@ss.hasPermi('problem:consult:edit')")
    @Log(title = "卡牌问题咨询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProblemConsult problemConsult)
    {
        return toAjax(problemConsultService.updateById(problemConsult));
    }

    /**
     * 删除卡牌问题咨询
     */
    @PreAuthorize("@ss.hasPermi('problem:consult:remove')")
    @Log(title = "卡牌问题咨询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
    	// 判断是否已经采纳了
    	int count = problemAnswerService.count(new LambdaQueryWrapper<ProblemAnswer>().eq(ProblemAnswer::getCid, id).eq(ProblemAnswer::getIsAdopt, 1));
    	if(count > 0) {
    		return AjaxResult.error("此问题已被采纳不可删除");
    	}
    	ProblemConsult consult = problemConsultService.getOne(new LambdaQueryWrapper<ProblemConsult>().eq(ProblemConsult::getId, id)
    			.select(ProblemConsult::getId,ProblemConsult::getIsDel,ProblemConsult::getUserId));
    	if(null == consult || 1 == consult.getIsDel()) {
    		return AjaxResult.error("此问题已被删除");
    	}
    	ProblemConsult problemConsult = new ProblemConsult();
    	problemConsult.setId(id);
    	problemConsult.setIsDel(1);
    	problemConsultService.updateById(problemConsult);
    	// 金额原路返回 - 删除定时任务
    	taskAsync.handleConsultDel(id,consult.getUserId());
        return AjaxResult.success("删除成功,金额将原路返回");
    }
    
    @PreAuthorize("@ss.hasPermi('problem:consult:edit')")
    @Log(title = "卡牌问题咨询", businessType = BusinessType.UPDATE)
    @DeleteMapping("/updateHide/{id}")
    public AjaxResult updateHide(@PathVariable Long id)
    {
    	ProblemConsult problemConsult = problemConsultService.getOne(new LambdaQueryWrapper<ProblemConsult>().eq(ProblemConsult::getId, id).select(ProblemConsult::getId,ProblemConsult::getIsHide));
    	int isHide = problemConsult.getIsHide() == 1 ? 0: 1;
    	problemConsult.setIsHide(isHide);
    	problemConsultService.updateById(problemConsult);
        return toAjax(1);
    }
}
